/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.apis.lighthouse.resources.events.ChangesetEvents;
	import be.wellconsidered.logging.Logger;
	
	public class Changesets extends AbstractResource
	{		
		private var _xmlNewChangeset:XML;
		
		public function Changesets(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List Changesets
		 */
		public function listChangesets(projectID:int):void
		{
			Logger.log("List Changesets");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/changesets.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onChangesetListingComplete, false, 0, true);
			
			function onChangesetListingComplete(e:GETEvents):void
			{
				dispatchEvent(new ChangesetEvents(ChangesetEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get Changeset
		 */
		public function getChangeset(projectID:int, changesetRevisionID:int):void
		{
			Logger.log("Get Changeset (" + ChangesetID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/changesets/" + changesetRevisionID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onChangesetFetchComplete, false, 0, true);
			
			function onChangesetFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new ChangesetEvents(ChangesetEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * New Changeset
		 */
		public function newChangeset(projectID:int):void
		{
			Logger.log("New Changeset");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/changesets/new.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onNewChangesetDataComplete, false, 0, true);
			
			function onNewChangesetDataComplete(e:GETEvents):void
			{
				_xmlNewChangeset = new XML(e.data);
				
				dispatchEvent(new ChangesetEvents(ChangesetEvents.NEW_COMPLETE, false, false, _xmlNewChangeset));	
			}
		}
		
		/**
		 * Create Changeset
		 */
		public function createChangeset(projectID:Number, changesetTitle:String, changesetBody:String, changesetArrayOfChanges:Array):void
		{
			Logger.log("Create Changeset");
			
			var xmlChangeset:XML = _xmlNewChangeset;
			
			xmlChangeset..title = changesetTitle;
			xmlChangeset..body = changesetBody;
			// xmlChangeset..changes = changesetArrayOfChanges; // TODO: YAML
			xmlChangeset..elements("changed-at").nodeValue = ;
			
			trace(xmlChangeset);
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/changesets.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlChangeset);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateChangesetComplete, false, 0, true);
			
			function onCreateChangesetComplete(e:POSTEvents):void
			{				
				dispatchEvent(new ChangesetEvents(ChangesetEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}

		/**
		 * Delete Changeset
		 */
		public function deleteChangeset(projectID:int, changesetRevisionID:int):void
		{
			Logger.log("Delete Changeset (" + ChangesetID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + "/changesets/" + changesetRevisionID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteChangesetComplete, false, 0, true);
			
			function onDeleteChangesetComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new ChangesetEvents(ChangesetEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}